/*
 * Copyright (C) 2009 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.inputmethod.pinyin;


import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;

/**
 * Switcher used to switching input mode between Chinese, English, symbol,etc.
 */
public class InputModeSwitcher {




    /**
     * Is return icon_setting in normal state?
     */
    private boolean mEnterKeyNormal = true;


    /**
     * IME service.
     */
    private PinyinIME mImeService;

    public LuaState state,lastState;
    public int keyBoardLayout;
    public enum LuaState{CN,JP,EN,NUM,SYM,SWITCH,EMOJI,CHAT}
    public enum KeyBoardState{NINE_KEY,FULL_KEY}
    public KeyBoardState keyBoardState;

    public class ThreadHandler extends Handler
    {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what)
            {
                case LOAD_DICT_CMP:
                    mImeService.mSkbContainer.updateInputMode();
                    break;
            }
            super.handleMessage(msg);
        }
    }
    ThreadHandler threadHandler = new ThreadHandler();
    public final static int LOAD_DICT_CMP = 154621;


    public static class KeyBoards{
        public static int EMOJI = R.layout.smile_cn;
        public static final int CN_NIKE_KEY = R.layout.ninekey_skb_cn;
        public static final int CN_FULL_KEY = R.layout.qwer_skb_cn;
        public static final int JP_NINE_KEY = R.layout.ninekey_skb_jp;
        public static final int JP_FULL_KEY = R.layout.qwer_skb_jp;
        public static final int EN_NINE_KEY = R.layout.ninekey_skb_en;
        public static final int EN_FULL_KEY = R.layout.qwer_skb_en;
        public static final int NUM = R.layout.ninekey_skb_num;
        public static final int SYM = R.layout.symbol_cn;
        public static final int CHAT_CN = R.layout.chat_cn;
        public static final int KEY_SWITCHER = R.layout.ninekey_qwer_switcher;
        public static boolean CAPS = false;
    }

    public class Dict{
        public static final int CN = R.raw.dict_cn;
        public static final int JP = R.raw.dict_jp;
        public static final int EN = R.raw.dict_en;
        public static final int COMMON = R.raw.common_dict;
    }

    public InputModeSwitcher(PinyinIME imeService) {
        mImeService = imeService;
        Resources r = mImeService.getResources();
        state = LuaState.CN;
        keyBoardState = KeyBoardState.NINE_KEY;
        keyBoardLayout = KeyBoards.CN_NIKE_KEY;
        lastState = state;
    }

    public int getSkbLayout() {
        return keyBoardLayout;
    }

    public void findDict(final int[] currentDicts, final boolean isQwer)
    {
        if((mImeService.tree =
                mImeService.treeMap.get(mImeService.sumInt(currentDicts)+""+isQwer)) == null)
        {
            mImeService.createLoadingView();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    TrieTree trieTree = new TrieTree();
                    mImeService.tree = trieTree;
                    int ids = mImeService.sumInt(currentDicts);
                    mImeService.treeMap.put(ids+""+isQwer,trieTree);
                    for(int id:currentDicts)
                    {
                        mImeService.buildTree(trieTree,id,isQwer);
                    }
                    Message message = new Message();
                    message.what =LOAD_DICT_CMP;
                    threadHandler.sendMessage(message);
                  }
            }).start();
        }else {
            mImeService.mSkbContainer.updateInputMode();
        }
    }

    public void changeKeyBoard(LuaState state,KeyBoardState keyBoardState)
    {
        this.state = state;
        if(state == LuaState.CN)
        {
            if(keyBoardState == KeyBoardState.NINE_KEY){
                mImeService.isQwer = false;
                keyBoardLayout = KeyBoards.CN_NIKE_KEY;
            }else
            {
                mImeService.isQwer = true;
                keyBoardLayout = KeyBoards.CN_FULL_KEY;
            }
            this.keyBoardState = keyBoardState;
            mImeService.currentDicts = new int[]{Dict.CN,Dict.COMMON};
            findDict(mImeService.currentDicts,mImeService.isQwer);
            lastState = state;
        }else if(state == LuaState.EN)
        {
            if(keyBoardState == KeyBoardState.NINE_KEY){
                mImeService.isQwer = false;
                keyBoardLayout = KeyBoards.EN_NINE_KEY;
            }else
            {
                mImeService.isQwer = true;
                keyBoardLayout = KeyBoards.EN_FULL_KEY;
            }
            this.keyBoardState = keyBoardState;
            mImeService.currentDicts = new int[]{Dict.EN};

            findDict(mImeService.currentDicts,mImeService.isQwer);
            lastState = state;
        }else if(state == LuaState.SWITCH)
        {
            keyBoardLayout = KeyBoards.KEY_SWITCHER;
            mImeService.mSkbContainer.updateInputMode();
        }else if(state == LuaState.JP)
        {
            if(keyBoardState == KeyBoardState.NINE_KEY){
                mImeService.isQwer = false;
                keyBoardLayout = KeyBoards.JP_NINE_KEY;
            }else
            {
                mImeService.isQwer = true;
                keyBoardLayout = KeyBoards.JP_FULL_KEY;
            }
            this.keyBoardState = keyBoardState;
            mImeService.currentDicts = new int[]{Dict.COMMON,Dict.JP};
            findDict(mImeService.currentDicts,mImeService.isQwer);
            lastState = state;
        }else if(state == LuaState.NUM)
        {
            keyBoardLayout = KeyBoards.NUM;
            mImeService.mSkbContainer.updateInputMode();
        }else if(state == LuaState.SYM)
        {
            keyBoardLayout = KeyBoards.SYM;
            mImeService.mSkbContainer.updateInputMode();
        }else if(state == LuaState.EMOJI)
        {
            keyBoardLayout = KeyBoards.EMOJI;
            mImeService.mSkbContainer.updateInputMode();
        }else if(state == LuaState.CHAT)
        {
            keyBoardLayout = KeyBoards.CHAT_CN;
            mImeService.mSkbContainer.updateInputMode();
            mImeService.setEmojiconFragment(Settings.getEmoji());
        }
    }

    // Return the icon to update.
    public boolean isChineseText() {
        return state == LuaState.CN;
    }
    public boolean isEnglishText() {
        return state == LuaState.EN;
    }
    public boolean isJapneseText() {
        return state == LuaState.JP;
    }
    public boolean isNumText() {
        return state == LuaState.NUM;
    }

    public boolean isEnterNoramlState() {
        return mEnterKeyNormal;
    }



  }
